package com.biz.primus.base.exception;

import com.biz.primus.base.constant.StatusCodes;
import org.apache.commons.lang3.ArrayUtils;

/**
 * Spring的事务管理默认只对出现运行期异常(java.lang.RuntimeException及其子类)进行回滚。
 * 如果一个方法抛出Exception或者Checked异常，Spring事务管理默认不进行回滚。
 * <p>
 * Created by defei on 7/31/17.
 */
public class BizSilentException extends RuntimeException implements BizException {

    private static final long serialVersionUID = -6403033409061476727L;

    public BizSilentException(String message) {
        super(message);
        this.description = message;
    }

    public BizSilentException(ExceptionType type, String message) {
        super(message);
        this.description = message;
        this.code = type.getCode();
    }

    public BizSilentException(ExceptionType type) {
        this(type,type.getDescription());
    }

    public BizSilentException(ExceptionType type, Object... args) {
        this(type, ArrayUtils.isNotEmpty(args) ? String.format(type.getDescription(), args) : type.getDescription());
    }


    public BizSilentException(int exceptionCode, String message) {

        super(message);
        this.code = exceptionCode;
        this.description = message;
    }

    public BizSilentException(int exceptionCode, String message, Throwable cause) {

        super(message, cause);
        this.code = exceptionCode;
        this.description = message;
    }

    public BizSilentException(String message, Throwable cause) {

        super(message, cause);
        this.description = message;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getDescription() {
        return description;
    }

    private Integer code = StatusCodes.COMMON_INTERNAL_EXCEPTION;

    private String description;
}
